package com.cf.common.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cf.common.mvp.BasePresenter;
import com.cf.common.mvp.BaseView;
import com.cf.common.rx.LifecycleRegistryToBehaviorSubject;
import com.cf.common.rx.RxEventSubject;
import com.trello.rxlifecycle2.android.ActivityEvent;

import io.reactivex.subjects.BehaviorSubject;

/**
 * @author yangji
 */
public abstract class BaseMvpViewActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView<P>, RxEventSubject {

    private LifecycleRegistryToBehaviorSubject subject = LifecycleRegistryToBehaviorSubject.create();

    @Override
    public BehaviorSubject<ActivityEvent> getSubject() {
        return subject.getSubject();
    }

    private P presenter = createPresenter();

    @Nullable
    public P getPresenter() {
        return presenter;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (presenter != null) {
            getLifecycle().addObserver(presenter);
        }
        getLifecycle().addObserver(subject);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getLifecycle().removeObserver(presenter);
        getLifecycle().removeObserver(subject);
        presenter = null;
        subject = null;
    }
}
